package com.budwk.app.sqfwV2.component;

import com.alibaba.fastjson.JSONObject;
import com.budwk.app.common.config.AppException;
import com.budwk.app.sqfw.component.FromSummaryComponent;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BasePdfGenerator;
import com.budwk.app.sqfwV2.dto.dto.OneFivePdfDTO;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.DownExcelLedgerReqVo;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.DownFileTypesResVo;
import com.budwk.app.sqfwV2.dto.vo.DocumentDownVo;
import com.budwk.app.sqfwV2.enums.FileServiceConfigEnum;
import com.budwk.app.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 12345代码功能类
 */

@Slf4j
public class OneFiveComponent {

    public static DownFileTypesResVo downDocFile(Sqfw_formSummary_new summaryNew, boolean isGeanerateWord, FileServiceConfigEnum serviceConfig) {
        DownExcelLedgerReqVo reqVo = new DownExcelLedgerReqVo();
        reqVo.setFileType(summaryNew.getType()).setAgainGenerate(isGeanerateWord);
        // 属于pdf模板
        PdfGenerateParam pdfGenerateParam = FromSummaryComponent.initPdfGenerateParam();
        OneFivePdfDTO pdfDTO = JSONObject.parseObject(summaryNew.getParamData(), OneFivePdfDTO.class);
        pdfGenerateParam.setAbstractCustomerPdfCreateField(pdfDTO);
        reqVo.setIds(List.of(summaryNew.getId()));
        // 初始化下载参数
        DocumentDownVo documentDownVo = new DocumentDownVo();
        documentDownVo.setReqVo(reqVo).setPdfGenerateParam(pdfGenerateParam).setFileName(serviceConfig.getFileName());
        try {
            BasePdfGenerator basePdfGenerator = StrUtil.getNewClass(serviceConfig.getService());
            documentDownVo.setBasePdfGenerator(basePdfGenerator);
        } catch (Exception e) {
            log.error("error: ", e);
            throw new AppException("数据异常");
        }
        return DocumentDownComponent.downWordDocFile(documentDownVo);
    }
}
